<?php

/**
 * ECSHOP 管理中心优惠活动管理
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: favourable.php 17217 2011-01-19 06:29:08Z liubo $
 */
 
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');

/*------------------------------------------------------ */
//-- 活动列表页
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'list')
{
    admin_priv('users_manage');


    /* 模板赋值 */
    $smarty->assign('full_page',   1);
    $smarty->assign('ur_here',     '提现记录');
    $smarty->assign('action_link', array('href' => 'mshop_pay_log.php?act=add', 'text' => '添加提现记录'));

    $list = mshop_pay_log_list();
// print_r($list);exit;

    $smarty->assign('mshop_pay_log_list', $list['item']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    /* 显示商品列表页面 */
    assign_query_info();
    $smarty->display('mshop_pay_log_list.htm');
}

/*------------------------------------------------------ */
//-- 分页、排序、查询
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
    $list = mshop_pay_log_list();

    $smarty->assign('mshop_pay_log_list', $list['item']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('mshop_pay_log_list.htm'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}
/*------------------------------------------------------ */
//-- 添加、编辑
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add' || $_REQUEST['act'] == 'edit')
{
    /* 检查权限 */
    admin_priv('users_manage');

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add';
    $smarty->assign('form_action', $is_add ? 'insert' : 'update');


    /* 初始化、取得优惠活动信息 */
    if ($is_add)
    {
        sys_msg('提现记录添加功能已屏蔽.');exit;

        // $mshop_pay_log = array(
        //     'pay_id'        => '',
        //     'pay_name'      => '',
        //     'pay_fee'      => 0,
        //     'pay_order'    => 30,
        //     'enabled'=>1 
        // );
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }
        $id = intval($_GET['id']);
        $mshop_pay_log = mshop_pay_log_info($id);
        if (empty($mshop_pay_log))
        {
            sys_msg($_LANG['favourable_not_exist']);
        }

    }
    $smarty->assign('mshop_pay_log', $mshop_pay_log);
 


    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', $_LANG['add_favourable']);
    }
    else
    {
        $smarty->assign('ur_here', $_LANG['edit_favourable']);
    }
    $href = 'mshop_pay_log.php?act=list';
    if (!$is_add)
    {
        $href .= '&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => $_LANG['favourable_list']));
    assign_query_info();
    $smarty->display('mshop_pay_log_info.htm');
}

/*------------------------------------------------------ */
//-- 添加、编辑后提交
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'insert' || $_REQUEST['act'] == 'update')
{
    /* 检查权限 */
    admin_priv('users_manage');

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert';

    /* 检查可提现积分 */
    $remark = $_POST['remark']? $_POST['remark']:"";
    $status = $_POST['status']?$_POST['status']: 0;
    $id = intval($_POST['id'])? intval($_POST['id']) : 0;

    

    /* 提交值 */
    $mshop_pay_log = array(
        'log_id'      => $id,
        'remark'      => $remark,
        'status'      => $status
    );




    /* 保存数据 */
    if ($is_add)
    {
        
        $db->autoExecute($ecs->table('mshop_pay_log'), $mshop_pay_log, 'INSERT');
        $mshop_pay_log['mshop_pay_log_id'] = $db->insert_id();
    }
    else
    {
        $lg_info = mshop_pay_log_info($id);
        $mshopinfo=mshopinfo($lg_info['user_id']);
        $mshop_id=$mshopinfo['mshop_id'];
        if($status!=$lg_info['status'])//是否变更状态
        {
            if($status==1)//完成
            {
              //之前状态取消
              if($lg_info['status']==2)
              {
                 $msinfo['sum_profit']=$mshopinfo['sum_profit']-$lg_info['pay_fee'];
                 $msinfo['sum_cash_amount']=$mshopinfo['sum_cash_amount']+$lg_info['pay_fee'];
              }
              else
              {
                  $mshop_pay_log['pay_time']=time();
                  //消减对应锁定佣金
                  $msinfo['lock_profit']=$mshopinfo['lock_profit']-$lg_info['pay_fee'];
                  //计算提现总金额
                  $msinfo['sum_cash_amount']=$mshopinfo['sum_cash_amount']+$lg_info['pay_fee'];            
              }
             


              $db->autoExecute($ecs->table('mshop'), $msinfo, 'UPDATE', "mshop_id = '$mshop_id'");
            }
            else if($status==2)//取消
            {
               //还原总积分
              $msinfo['sum_profit']=$mshopinfo['sum_profit']+$lg_info['pay_fee'];
               //提现总金额
              $msinfo['sum_cash_amount']=$mshopinfo['sum_cash_amount']-$lg_info['pay_fee'];
              $db->autoExecute($ecs->table('mshop'), $msinfo, 'UPDATE', "mshop_id = '$mshop_id'");

            }
            else//重新设置未确认
            {
               //取消后
              if($lg_info['status']==2)
              {
                 $msinfo['sum_profit']=$mshopinfo['sum_profit']-$lg_info['pay_fee'];
                 $msinfo['lock_profit']=$mshopinfo['lock_profit']+$lg_info['pay_fee'];

              }
              else
              {
                  $msinfo['sum_cash_amount']=$mshopinfo['sum_cash_amount']-$lg_info['pay_fee'];
                  $msinfo['lock_profit']=$mshopinfo['lock_profit']+$lg_info['pay_fee'];            
              }  


              $db->autoExecute($ecs->table('mshop'), $msinfo, 'UPDATE', "mshop_id = '$mshop_id'");
            }

        }

        $db->autoExecute($ecs->table('mshop_pay_log'), $mshop_pay_log, 'UPDATE', "log_id = '$id'");
         
    }

    /* 记日志 */
    if ($is_add)
    {
        admin_log('审核提现记录'.$mshop_pay_log['log_id'], 'add', 'mshop_pay_log');
    }
    else
    {
        admin_log('修改提现审核'.$mshop_pay_log['log_id'], 'edit', 'mshop_pay_log');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'mshop_pay_log.php?act=add', 'text' => $_LANG['continue_add_favourable']),
            array('href' => 'mshop_pay_log.php?act=list', 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg($_LANG['add_favourable_ok'], 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'mshop_pay_log.php?act=list&' . list_link_postfix(), 'text' => $_LANG['back_favourable_list'])
        );
        sys_msg($_LANG['edit_favourable_ok'], 0, $links);
    }
}

/*------------------------------------------------------ */
//-- 搜索商品
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'search')
{
    /* 检查权限 */
    check_authz_json('mshop_pay_log');

    include_once(ROOT_PATH . 'includes/cls_json.php');

    $json   = new JSON;
    $filter = $json->decode($_GET['JSON']);
    $filter->keyword = json_str_iconv($filter->keyword);
    if ($filter->act_range == FAR_ALL)
    {
        $arr[0] = array(
            'id'   => 0,
            'name' => $_LANG['js_languages']['all_need_not_search']
        );
    }
    else
    {
       $arr=$mshop_pay_log_list;
    }

    if (empty($arr))
    {
        $arr = array(0 => array(
            'id'   => 0,
            'name' => $_LANG['search_result_empty']
        ));
    }

    make_json_result($arr);
}

/*
 * 取得店铺列表
 * @return   array
 */
function mshop_pay_log_list()
{
    $result = get_filter();

    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
        $filter['user_name']    = empty($_REQUEST['user_name']) ? '' : trim($_REQUEST['user_name']);
        $filter['status']    = empty($_REQUEST['status']) ? '' : intval($_REQUEST['status']);
        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
            $filter['user_name'] = json_str_iconv($filter['user_name']);
            $filter['status'] = json_str_iconv($filter['status']);
        }
       

        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'p.log_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $where = "";
        if (!empty($filter['keyword']))
        {
            $where .= " AND p.pay_no LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
        }
        if (!empty($filter['user_name']))
        {
            $where .= " AND u.user_name LIKE '%" . mysql_like_quote($filter['user_name']) . "%'";
        }
         if (!empty($filter['status']))
        {
            $filter['status']=$filter['status']-1;//0代表等待审核
            $where .= " AND p.status=" . $filter['status'];
        }
        $ts_where=" and u.mshop_id!=0";//没有店铺的会员不用连接
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('mshop_pay_log')." as p"
              ." left join ".$GLOBALS['ecs']->table('users')."  as u on u.user_id=p.user_id "
              ." WHERE 1 $where".$ts_where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        

        /* 分页大小 */
        $filter = page_and_size($filter);

        // $filter['page_size']=15;//测试分页效果
        // $filter['page_count']=1;
        // print_r($__REQUEST);exit();


        /* 查询 */

        $sql =  " SELECT p.*"
                .",u.user_id,u.user_name,u.mobile_phone "
               ."FROM " . $GLOBALS['ecs']->table('mshop_pay_log')." as p"
                ." left join ".$GLOBALS['ecs']->table('users')."  as u on u.user_id=p.user_id "
                ." WHERE 1 $where ".$ts_where
                ." ORDER BY $filter[sort_by] $filter[sort_order] "
                ." LIMIT ".$filter['start'] .", $filter[page_size]";

        $filter['keyword'] = stripslashes($filter['keyword']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->query($sql);





    $list = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        
        $row['ctime']=date("Y-m-d H:i:s",$row['ctime']);
        if($row['pay_time'])
        $row['pay_time']=date("Y-m-d H:i:s",$row['pay_time']);
        $list[]=$row;
    }

    return array('item' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

   function mshop_pay_log_info($id)
   {
       //大改数据表，提现账户可变，提现记录不可随之改变
       
       $sql = " SELECT p.*,u.user_id,u.user_name,u.mobile_phone "
        ."FROM " . $GLOBALS['ecs']->table('mshop_pay_log')." as p"
        ." left join ".$GLOBALS['ecs']->table('users')."  as u on u.user_id=p.user_id "
        ." where p.log_id=".$id;


        $row=$GLOBALS['db']->getRow($sql);

        $row['ctime']=date("Y-m-d H:i:s",$row['ctime']);
		if($row['pay_time'])
        $row['pay_time']=date("Y-m-d H:i:s",$row['pay_time']);

        return $row;
   }
   function mshopinfo($user_id)
   {
  
       $sql = " SELECT m.*,u.user_name,u.mobile_phone "
        ."FROM " . $GLOBALS['ecs']->table('users')." as u"
        ." left join ".$GLOBALS['ecs']->table('mshop')."  as m on m.mshop_id=u.mshop_id "
        ." where u.user_id=".$user_id;


        $row=$GLOBALS['db']->getRow($sql);
        return $row;
   }


?>